package com.bjpowernode.config.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.bjpowernode.config.context.ApplicationContextProvider;
import com.bjpowernode.constant.Constants;
import com.bjpowernode.entity.TDicType;
import com.bjpowernode.entity.TDicValueDO;
import com.bjpowernode.entity.TProductDO;
import com.bjpowernode.service.DicTypeService;
import com.bjpowernode.service.ProductService;

import java.util.List;

public class IntentionProductConverter implements Converter<Integer> {

    @Override
    public Integer convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        int result = -1; //-1表示数据库不存在该"产品"值
        //拿到产品的中文名称：“比亚迪EV”、“秦DM-I”
        String intentionProductName = cellData.getStringValue();
        //把产品的中文名称 转成 对应的id值

        //拿到spring容器中的DicTypeService对象，然后调用里面的查询“产品”字典值的方法，拿到称呼的字典数据
        ProductService productService = ApplicationContextProvider.getBean(ProductService.class);
        List<TProductDO> tProductDOList = productService.getOnSaleProduct(); //查询在售的产品，售罄的不需要查

        for (TProductDO tProductDO : tProductDOList) {
            Integer id = tProductDO.getId();
            String name = tProductDO.getName();
            if (intentionProductName.equals(name)) {
                result = id;
                break;
            }
        }
        return result;
    }
}
